package live.cz.response;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import live.common.MemCached;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

@SuppressWarnings("unchecked")
public class XmlFormart {

	private static String result = null;
	private static String PROD_TXT_URL = XmlFormart.class.getResource("/")
			.getPath()
			+ "Products.dat";
	private final static File file = new File("E://product.xml");

	public static Document xmlToDocument(String xmlStr)
			throws DocumentException {
		return DocumentHelper.parseText(xmlStr.trim());
	}

	public static String returnXmlNodeValue(Element e, String nodeName,
			String qName) throws UnsupportedEncodingException {
		String xpath = new StringBuffer("./").append(nodeName).append(
				"[@name='").append(qName).append("']").toString();
		result = ((Element) e.selectSingleNode(xpath)).attributeValue("value");
		return decoedUTF(result);
	}

	public static List returnElements(Document doc, String xpath)
			throws UnsupportedEncodingException {
		List results = null;
		List nodes = doc.selectNodes(xpath);
		if (nodes != null && nodes.size() > 0) {
			results = new ArrayList();
			Iterator iter = nodes.iterator();
			while (iter.hasNext()) {
				Element e = (Element) iter.next();
				results.add(e);
			}
		}
		return results;
	}

	public static String encodeUTF(String str)
			throws UnsupportedEncodingException {
		return URLEncoder.encode(str, "UTF-8");
	}

	public static String decoedUTF(String str)
			throws UnsupportedEncodingException {
		return URLDecoder.decode(str, "UTF-8");
	}

	/**
	 * @获取产品信息
	 * @param str
	 * @return
	 */
	public static String returnProducts(String str) {
		StringBuffer buffer = new StringBuffer();
		try {
			MemCached mcc = MemCached.getInstance();
			String products = "";
			if (mcc.get("products") != null) {
				products = mcc.get("products").toString();
			} else {
				buffer = new StringBuffer();
				InputStreamReader ir = new InputStreamReader(
						new FileInputStream(file));
				BufferedReader reader = new BufferedReader(ir);
				while ((result = reader.readLine()) != null) {
					buffer.append(result);
				}
				reader.close();
				ir.close();
			}
			buffer.append(products);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return buffer.toString();
	}

	/**
	 * @产品信息写入文件
	 * @param str
	 * @throws Exception
	 */
	public static void writeProductInfo(String str) throws Exception {
		OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(
				file));
		write.write(str);
		write.close();
	}
}
